package com.house.java.action.user;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.house.java.biz.UserBusiness;
import com.house.java.model.User;
import com.house.java.util.StringUtils;

public class UserAction {

	public void queryUserByCondition(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, UnsupportedEncodingException{
		request.setCharacterEncoding("utf-8");
		String idStr = request.getParameter("id");
		Long id = null;
		if(StringUtils.hasLength(idStr)){
			id = Long.valueOf(idStr);
		}
		String userName = request.getParameter("userName");
		Integer currentPage = StringUtils.hasLength(request.getParameter("currentPage"))?Integer.valueOf(request.getParameter("currentPage")):null;
		Integer pageSize = StringUtils.hasLength(request.getParameter("pageSize"))?Integer.valueOf(request.getParameter("pageSize")):null;
		// 查询数据
		User user = new User();
		user.setId(id);
		user.setUserName(StringUtils.hasLength(userName)?null:userName);
		UserBusiness biz = new UserBusiness();
		List<User> list = biz.queryUsers(user, currentPage, pageSize);
		// 将list集合对象绑定到request区域中
		// 通过request.setAttribute(绑定名字，集合变量)；方法绑定
		request.setAttribute("users",list);
	}
	
	
	public void queryUserById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, UnsupportedEncodingException{
		request.setCharacterEncoding("utf-8");
		String idStr = request.getParameter("id");
		Long id = null;
		if(StringUtils.hasLength(idStr)){
		id = Long.valueOf(idStr);
		}
		UserBusiness biz = new UserBusiness();
		List<User> lis = biz.queryUsers(id, null, null);
		// 将list集合对象绑定到request区域中
		// 通过request.setAttribute(绑定名字，集合变量)；方法绑定
		request.setAttribute("user",lis.get(0));
		}
}
